package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.engineering.customview.ReadDilalog;
import com.cloudsigns.factorymanager2.project.activity.AddBatchInstallationOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringInfomartionActivity;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringInformationAdapter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;


public class EditBatchInstallationEngineeringDetailActivity1 extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView img_back;
    private TextView tv_title;
    private EditText edt_completion_date;
    private ImageView iv_completion_date,img_edit_engineering_information;
    private ListView lv;
    private EngineeringInformationAdapter adapter;
    private List<EngineeringInstallationOrderBean> engineeringBeanList = new ArrayList<>();
    private final String TAG = EditBatchInstallationEngineeringDetailActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_edit_batch_installation_engineering_detail1);

        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        edt_completion_date=findViewById(R.id.edt_completion_date);
        iv_completion_date=findViewById(R.id.iv_completion_date);
        img_edit_engineering_information=findViewById(R.id.img_edit_engineering_information);
        lv = findViewById(R.id.lv);


        tv_title.setText(getResources().getText(R.string.work_order_information));

        img_back.setOnClickListener(this);
        iv_completion_date.setOnClickListener(this);
        img_edit_engineering_information.setOnClickListener(this);

        engineeringBeanList.add(new EngineeringInstallationOrderBean());
        engineeringBeanList.add(new EngineeringInstallationOrderBean());
        engineeringBeanList.add(new EngineeringInstallationOrderBean());
        engineeringBeanList.add(new EngineeringInstallationOrderBean());


        adapter = new EngineeringInformationAdapter(this, TAG);
        adapter.setData(engineeringBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(this);
    }


    // 完成日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            // 完成日期
            case R.id.iv_completion_date:
                showDateDialog();
                break;
            // 工程信息
            case R.id.img_edit_engineering_information:
                mIntent = new Intent(EditBatchInstallationEngineeringDetailActivity1.this, AddEngineeringInfomartionActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_LIST);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_LIST: //添加产品清单
                if (data != null) {
                    EngineeringInstallationOrderBean engineeringBean = (EngineeringInstallationOrderBean) data.getSerializableExtra(ConstantUtil.ENGINEERING_INFO);

                    engineeringBeanList.add(engineeringBean);
                    adapter.setData(engineeringBeanList);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}
